package com.yfp.operation.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfp.common.security.utils.SecurityUtils;
import com.yfp.operation.domain.DeviceCurrentComboPlanRoadDO;
import com.yfp.operation.domain.MachineDamageDO;
import com.yfp.operation.mapper.MachineDamageMapper;
import com.yfp.operation.service.MachineDamageDetailService;
import com.yfp.operation.service.MachineDamageService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 * @description 针对表【machine_damage】的数据库操作Service实现
 * @createDate 2024-04-25 10:00:41
 */
@Service
@Slf4j
public class MachineDamageServiceImpl extends ServiceImpl<MachineDamageMapper, MachineDamageDO> implements MachineDamageService {

    @Resource
    private MachineDamageDetailService machineDamageDetailService;

    /**
     * 报损数据提交
     *
     * @param damageDTOS
     * @return
     */
    @Transactional
    @Override
    public Boolean damageHandle(List<DeviceCurrentComboPlanRoadDO> damageDTOS) {
        String machineId = CollUtil.getFirst(damageDTOS).getMachineId();
        DateTime date = DateUtil.date();

        MachineDamageDO machineDamageDO = new MachineDamageDO();
        machineDamageDO.setMachineId(machineId);
        machineDamageDO.setUserName(SecurityUtils.getUsername());
        machineDamageDO.setCreatTime(date);
        machineDamageDO.setUpdateTime(date);
        this.save(machineDamageDO);

        log.info("设备id:{},最终报损数据:{}",machineId,damageDTOS);
        return machineDamageDetailService.damageHandle(damageDTOS);
    }
}




